<template>
  <div class="login_container">
    <div class="login_box">
      <!--头像区域  -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt />
      </div>
      <!-- 表单登录区域 -->
      <!-- 不能使用v-model指令 坑：使用后无法进行数据的验证-->
      <el-form
        ref="loginFormRef"
        label-width="0px"
        :rules="loginFormRules"
        :model="loginForm"
        class="login_form"
      >
        <el-form-item prop="username">
          <el-input
            prefix-icon="iconfont icon-yonghu"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            prefix-icon="iconfont icon-mima"
            v-model="loginForm.password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
          <!-- <el-button type="primary" @click="register">注册</el-button> -->
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单的验证规则
      loginFormRules: {
        username: [
          //require 代表必填项
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login() {
      //调用validata()实现表单的预验证 传一个回调函数回调函数的参数布尔值
      this.$refs.loginFormRef.validate(async valid => {
        //console.log(valid)  valid是布尔值
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.meta.status !== 200) return this.$message.error('登录失败')
        this.$message.success('登录成功')
        /**
         * 1.将登陆成功后的token 存入seesionStorage 会话存储
         * 1.1项目中除登陆API的其他接口 必须在登录后访问
         * 1.2token只应在当前网站打开期间生效 ，所以token存储在seesionStorage
         */
        window.sessionStorage.setItem('token', res.data.token)

        //2.通过编程式导航 跳转到后台主页
        this.$router.push('/home')
      })
    },
    // register(){
    //   this.$dispath('/register')
    // },
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields() //通过ref获取表单实例 调用element-ui表单自带的resetFields()方法重置表单
    }
  }
}
</script>
<style lang="less" scoped>
.login_container {
  background-color: #bdb76b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background: #fff;
  border-radius: 3px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.avatar_box {
  width: 130px;
  height: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  position: absolute;
  padding: 10px;
  box-shadow: 0, 0, 10px, #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #fff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background: #eee;
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding-left: 20px;
  padding-right: 20px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
